<template>
	<!-- 统计报表-新契约工作量页面 -->
	<div class="fillcontain">
		<!-- 标题 -->
		<div class="fastQuery">
			<el-row>
				<el-col :span="6" class="fastQuery_title">
					<i class="el-icon-tickets"></i><span>新契约工作量报表</span></el-col>
				<el-col :span="8"></el-col>
			</el-row>
		</div>
		<el-form :model="searchParams" ref="ruleForm" label-width="100px" class="search_container">
			<el-row>
				<el-col :span="1">&nbsp;</el-col>
				<el-col :span="5">
					<el-form-item label="统计选项">
						<el-select @change="changeSelect" v-model="searchParams.type" placeholder="统计选项">
							<el-option v-for="item in statTypes" :key="item.value" :label="item.name" :value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="是否当天" v-if="searchParams.type == 'all'">
						<el-radio-group @change="changeToday" v-model="isToday" columns="2">
							<el-radio-button label="1" selected>是</el-radio-button>
							<el-radio-button label="0">否</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="统计时间" prop="idNo" v-if="searchParams.type == 'all'">
						<el-date-picker :disabled="isToday=='1'" @change="getTime" :picker-options="datePicker1"
						 v-model="time" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd" range-separator="至"
						 start-placeholder="开始日期" end-placeholder="结束日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="统计时间" prop="idNo" v-if="searchParams.type != 'all'">
						<el-date-picker  @change="getTime" :picker-options="datePicker"
						 v-model="time" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd" range-separator="至"
						 start-placeholder="开始日期" end-placeholder="结束日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="4" class="text_center">
					<el-button type="primary" icon="el-icon-search" @click="search"></el-button>
					<el-button type="primary" plain icon="el-icon-refresh" @click="resetSearch"></el-button>
				</el-col>
			</el-row>
		</el-form>
		<div class="contain">
			<!-- 操作按钮 -->
			<el-row class="bgfff pb20">
				<el-col :span="24">
					<el-button @click="exportExcel" type="danger" plain>导出</el-button>
				</el-col>
			</el-row>
			<div class="table_container">
				<el-table ref="tableRef" v-loading="loading" :data="tableData" highlight-current-row header-cell-class-name="table-header-class"
				 style="width:100%">
					<el-table-column min-width="120" property="allCount" label="工作量" align='center' show-overflow-tooltip>
					</el-table-column>
					<!-- <el-table-column min-width="120" v-if="false"  property="ratio" label="工作效率" align='center' show-overflow-tooltip>
					</el-table-column> -->
					<el-table-column label="任务明细" align='center' show-overflow-tooltip>
						<el-table-column min-width="120" prop="nbCount" label="新单核保" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="bqCount" label="保全核保" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="lpCount" label="理赔二核" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="zlCount" label="资料修正" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="xqCount" label="续期核保" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="zbCount" label="再保处理" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="cbCount" label="承保后调查" align='center' show-overflow-tooltip>
						</el-table-column>
						<el-table-column min-width="120" prop="yhbCount" label="预核保" align='center' show-overflow-tooltip>
						</el-table-column>
					</el-table-column>
				</el-table>
			</div>
		</div>
	</div>
</template>
<script>
	import moment from 'moment';
	import TaskConstants, {
		call,
		exportData
	} from "@/api/taskManager/common"
	import constant from '@/utils/constant';
	export default {
		data() {
			return {
				statTypes: [{
						name: "未处理任务数",
						value: "Untreated",
					},
					{
						name: "任务总数",
						value: "all",
					}
				],
				loading: false,
				tableData: [],
				now: new Date(),
				time: '', //初始日期
				isToday: '1', //是否当天 1是 0 否
				searchParams: {
					startTime: '',
					endTime: '',
					type: 'Untreated',
					isToday: '1'
				},
				datePicker: {
					onPick: (obj) => {
						this.pickerMinDate = new Date(obj.minDate).getTime();
						console.log(obj)
					},
					disabledDate: (time) => {
						let current = new Date();
						if (this.pickerMinDate) {
							const day1 = 6 * 24 * 3600 * 1000
							let maxTime = this.pickerMinDate + day1
							let minTime = this.pickerMinDate - day1
							return time.getTime() > maxTime || time.getTime() < minTime || time.getTime() > current.getTime();
						} else {
							return time.getTime() > current.getTime();
						}
						// /* 只能查询前一天的数据 */
						// let currTime = moment().add(-1, 'days');
						// return time.getTime() >= currTime.toDate().getTime();
					}
				},
				datePicker1: {
					disabledDate: (time) => {
						// /* 只能查询前一天的数据 */
						let currTime = moment().add(-1, 'days');
						return time.getTime() >= currTime.toDate().getTime();
					}
				},
				type: "all",
				title: '', //传入后端的title
			}
		},
		created() {
			let now = moment().format("YYYY-MM-DD");
			this.time = [now, now];
			this.searchParams.startTime = now;
			this.searchParams.endTime = now;
		},
		activated() {
			document.onkeydown = (event) => {
				var e = event || window.event || arguments.callee.caller.arguments[0];
				if (e && e.keyCode == 113) {
					this.search();
				}
			};
		},
		methods: {
			show() {
				this.$store.dispatch('loading/CHANGE_LOADING', true);
			},
			cancle() {
				this.$store.dispatch('loading/CHANGE_LOADING', false)
			},
			getTime(arrayTime) {
				if (arrayTime != null) {
					this.searchParams.startTime = arrayTime[0];
					this.searchParams.endTime = arrayTime[1];
					this.title = this.searchParams.startTime + ' 到 ' + this.searchParams.endTime;
				}
			},
			changeSelect(value) {
				this.tableData=[];
				if(value == 'all' && this.searchParams.isToday == '1') {
					let now = moment().format("YYYY-MM-DD");
					this.searchParams.startTime = now;
					this.searchParams.endTime = now;
					this.time = [now, now];
				} else {
					this.searchParams.startTime = '';
					this.searchParams.endTime = '';
					this.time = ['', ''];
				}
			},
			changeToday(value) {
				if (value == '1') {
					this.searchParams.isToday = '1';
					let now = moment().format("YYYY-MM-DD");
					this.searchParams.startTime = now;
					this.searchParams.endTime = now;
					this.time = [now, now];
				} else {
					this.searchParams.startTime = '';
					this.searchParams.endTime = '';
					this.time = ['', ''];
					this.searchParams.isToday = '0';
				}
			},
			search() {
				if (this.searchParams.startTime == '' || this.searchParams.startTime == undefined || this.searchParams.startTime ==
					'undefined') {
					this.$message.error("请选择时间！");
					return;
				}
				if (this.searchParams.type != 'all') {
					this.searchParams.isToday = '1';
				}
				this.show();
				call(TaskConstants.apis.queryWork, this.searchParams, "post").then(result => {
					if (result.resultCode == constant.SUCCESS && result.data) {
						let resData = result.data;
						this.tableData = resData.resData;
					}
					this.cancle();
				}).catch(err => {
					this.cancle();
				});
			},
			resetSearch() {
				this.time = "";
			},
			exportExcel() {
				let titleArrayAll = []; //总的title
				let titleFirst = []; //总的title
				let titleSecond = []; //总的title
				let titleMager = []; //总的title
				let data = this.$refs.tableRef.data; //table数据
				// if (undefined == data || data.length == 0) {
				// 	this.$message.error("请先查询数据！");
				// 	return;
				// }
				this.$refs.tableRef.columns.forEach(item => {
					let ite = {};
					ite[item.property] = item.label;
					if (item.level == 1) {
						titleFirst.push(ite);
					}
					if (item.level == 2) {
						titleSecond.push(ite);
					}
				});
				titleMager.push("任务明细");
				if (this.title != undefined || "" != this.title) {
					titleMager.push(this.title);
				}
				//excel的表标题
				titleArrayAll = [titleFirst, titleSecond];
				let param = {
					tableTitle: titleArrayAll,
					titleMager: titleMager,
					data: data,
					title: "新契约工作量报表",
					type: this.searchParams.type,
					html: 'work'
				}
				param = JSON.stringify(param);
				let vueObj = this;
				exportData(vueObj, TaskConstants.apis.exportRatio, {
					allData: param
				}, "post", "新契约工作量报表.xls", "application/vnd.ms-excel;charset=utf-8");
			}
		}
	}
</script>
<style lang="less" scoped>
	.fillcontain {
		padding-bottom: 0;

		.fastQuery {
			padding: 15px 20px 10px 20px;
			background: #fff;
			border-bottom: 1px solid #e1e1e1;

			.fastQuery_title {
				color: #666;
				padding: 6px 0px;
			}
		}
	}

	.contain {
		background: #fff;
		padding: 10px;
		margin-bottom: 20px;
	}
</style>
